package com.example.qu_tao.View;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qu_tao.R;
import com.example.qu_tao.adapter.MessageAdapter;
import com.example.qu_tao.entity.Message;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity {

    private RecyclerView messageRecyclerView;
    private MessageAdapter messageAdapter;
    private EditText messageInput;
    private ImageButton sendButton, backButton;
    private String receiverName;
    private List<Message> messageList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        // 获取传递的用户名
        Intent intent = getIntent();
        receiverName = intent.getStringExtra("name");
        setTitle(receiverName);

        // 初始化视图组件
        messageRecyclerView = findViewById(R.id.messageRecyclerView);
        messageInput = findViewById(R.id.messageInput);
        sendButton = findViewById(R.id.sendButton);
        backButton = findViewById(R.id.backButton); // 获取返回按钮

        // 配置RecyclerView
        messageRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        messageAdapter = new MessageAdapter(messageList);
        messageRecyclerView.setAdapter(messageAdapter);

        // 设置发送按钮点击事件
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });

        // 设置返回按钮点击事件
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(); // 返回到上一个页面
            }
        });

        // 模拟一些历史消息
        initMockMessages();
    }

    private void sendMessage() {
        String content = messageInput.getText().toString().trim();
        if (!content.isEmpty()) {
            // 创建新消息并添加到列表
            Message newMessage = new Message(content, Message.TYPE_SENT);
            messageList.add(newMessage);

            // 更新UI显示新消息
            messageAdapter.notifyDataSetChanged();
            scrollToBottom();

            // 清空输入框
            messageInput.setText("");
        }
    }

    private void scrollToBottom() {
        // 滚动到RecyclerView底部
        messageRecyclerView.scrollToPosition(messageAdapter.getItemCount() - 1);
    }

    private void initMockMessages() {
        // 添加一些模拟的历史消息
        messageList.add(new Message("你好，今天过得怎么样？", Message.TYPE_RECEIVED));
        messageList.add(new Message("我觉得特别棒！！！", Message.TYPE_SENT));

        // 更新适配器
        messageAdapter.notifyDataSetChanged();
        scrollToBottom();
    }
}